<script setup lang="ts">
import { ref, reactive, computed, provide, defineExpose, defineProps, defineEmits } from 'vue';
import BaseTableSearch from './BaseTableSearch.vue'; // 假设这是你的搜索组件
import BaseTable from './BaseTable.vue'; // 假设这是你的表格组件

const props = defineProps({
  tableConfig: {
    type: Object,
    default: () => ({})
  },
  searchConfig: {
    type: Object,
    default: () => ({})
  },
  handlers: {
    type: Object,
    default: () => ({})
  }
})

const tableRef = ref(null as any);
const searchRef = ref(null as any);

const emit = defineEmits(['search-complete', 'reset-complete'])
const state = reactive({
  loading: false,
  queryParams: {
    initParam: {},
    search: {},
    tableParams: {},
    pagination: {
      _current: 1,
      _size: 10
    }
  },
  lastSearchParams: {}
})



const mergedSearchConfig = computed(() => ({
  columnCount: 4,
  expandLimit: 6,
  autoFit: true,
  columnWidth: '300px',
  labelPosition: 'left',
  labelWidth: '100px',
  fields: [
    {
      type: 'date-range',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      label: '日期范围',
      field: 'dateRange'
    }
  ],
  buttons: [
    {
      name: '搜索',
      type: 'primary',
      handler: 'submit'
    },
    {
      name: '重置',
      handler: 'reset'
    }
  ],
  ...props.searchConfig
}))

const mergedTableConfig = computed(() => ({
  border: true,
  pagination: false,
  inline: true,
  selectable: true,
  isFullscreen: false,
  minWidth: '200px',
  ...props.tableConfig
}))





const defaultHandlers = {
  // 参数转换（处理日期范围等）
  transformParams(params) {
    if (params.dateRange) {
      return {
        ...params,
        startTime: params.dateRange[0],
        endTime: params.dateRange[1]
      }
    }
    return params
  },
  updateInitParams(params) {
    state.queryParams.initParam = params
  },
  // 更新搜索参数
  updateSearchParams(params) {
    state.queryParams.search = params
  },

  setToolbarParam: (key: string, value: any) => {
    if (state.queryParams && state.queryParams.tableParams) {
      state.queryParams.tableParams[key] = value
    }
  },

  // 更新分页参数
  updatePagination(params) {
    state.queryParams.pagination = { ...state.queryParams.pagination, ...params }
  },

  refresh() {
    this.execute()

  },

  getRequestParams() {
    return state.lastSearchParams
  },

  getXlsTitle() {
    return tableRef.value.getXlsTitle()
  },


  async execute(extraParams = {}) {
    state.loading = true
    try {
      const params = {
        ...state.queryParams.initParam,
        ...state.queryParams.search,
        ...state.queryParams.tableParams,
        ...state.queryParams.pagination,
        ...extraParams
      }

      const processedParams = this.transformParams(params)
      const result = await tableRef.value?.loadData(processedParams)
      state.lastSearchParams = processedParams

      return result
    } finally {
      state.loading = false
    }
  }


}

// 合并自定义处理器
const runtimeHandlers = {
  ...defaultHandlers,
  ...props.handlers
}

// 提供全局方法
provide('searchTablePageContext', {
  state,
  handlers: runtimeHandlers,
  refresh: () => runtimeHandlers.execute()
})

// 暴露API
defineExpose({
  search: runtimeHandlers.execute,
  reset: runtimeHandlers.reset,
  getParams: () => state.searchParams,
  getTableRef: () => tableRef.value,
  getSearchRef: () => searchRef.value
})



</script>

<template>
  <BaseTableSearch ref="searchRef" v-model="mergedSearchConfig" />
  <BaseTable ref="tableRef" v-model="mergedTableConfig" />
</template>

<style scoped lang="scss"></style>
